package com.yejhxt.tools;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.yejhxt.api.SessionContext;

public class ProjectTools {
	public static void ResponseOut(HttpServletResponse response, String json) {
		response.setContentType("text/json; charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.write(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static String getMessage(String phoneNum) throws Exception {
		int validateCode = (int) (Math.random() * 10000);
		// TaobaoClient client = new
		// DefaultTaobaoClient("http://gw.api.taobao.com/router/rest",
		// "23339176", "0b60225d5d16ec980be1289969fbb449");
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
//		TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23339176",
//				"0b60225d5d16ec980be1289969fbb449");
		TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23289454",
				"86a0633a94ed04b6cf98607cc5351bdb");
		
		req.setExtend("123456");
		req.setSmsType("normal");
		req.setSmsFreeSignName("身份验证");
		req.setSmsParamString("{\"code\":\"" + validateCode + "\",\"product\":\"幼儿监护系统\"}");
		req.setRecNum(phoneNum);
		//req.setSmsTemplateCode("SMS_7780400");
		req.setSmsTemplateCode("SMS_34380149");
		
		AlibabaAliqinFcSmsNumSendResponse rsp;

		rsp = client.execute(req);
		 System.out.println(rsp.getBody());
		
		//System.out.println(validateCode);
		return validateCode + "";
	}

	public static boolean isPhone(String phone) {
		String pattern = "0?(13|14|15|18)[0-9]{9}";
		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(phone);
		return m.matches();
	}

	public static Map<String, Object> returnMap(int status) {
		Map<String, Object> map = new HashMap<String, Object>();
		switch (status) {
		case 200:
			map.put("status", "success");
			map.put("code", status);
			break;
		case 403:
			map.put("status", "failure");
			map.put("code", status);
			map.put("error_description", "Forbidden");
			break;
		case 404:
			map.put("status", "failure");
			map.put("code", status);
			map.put("error_description", "Not Found");
		case 406:
			map.put("status", "failure");
			map.put("code", status);
			map.put("error_description", "Not Acceptable");
			break;
		case 422:
			map.put("status", "failure");
			map.put("code", status);
			map.put("error_description", "Unprocessable Entity");
			break;
		case 500:
			map.put("status", "failure");
			map.put("code", status);
			map.put("error_description", "Internal Server Error");
			break;
		}
		return map;
	}

	public static HttpSession getAppSession(String vaidatecodeid) {
		SessionContext sessionContext = SessionContext.getInstance();
		return sessionContext.getSession(vaidatecodeid);
	}

	public static boolean isID(String id) {
		
		String pattern = "\\d{17}[\\d|xX]|\\d{15}";
		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(id);
//		System.out.println(id+m.matches());
		return m.matches();

	}
}
